package com.wuwei.jy.controller;

import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.wuwei.dto.ComResult;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by admin on 2017/4/7.
 */


@Controller
@RequestMapping("/upload")
public class UploadFileController {


    @ResponseBody
    @RequestMapping("/uploadFile")
    public ComResult<String> fileUpload(HttpServletRequest request){
        String accessKey = "9Y-lXWKSowFMSqQ-jWeW5BZYx7xY4ytww5WkLkGC";
        String secretKey = "8T3ed4mY4lxlPa0jFCrW7xSv8y2KtKFDIbnlWuqg";
        String bucket = "wuwei-yunzhuan";
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        System.out.println(upToken);

        try{
            //使用Apache文件上传组件处理文件上传步骤：
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
            //解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8");
            //3、判断提交上来的数据是否是上传表单的数据
            if(!ServletFileUpload.isMultipartContent(request)){
                //按照传统方式获取数据
                return null;
            }
            //4、使用ServletFileUpload解析器解析上传数据，解析结果返回的是一个List<FileItem>集合，每一个FileItem对应一个Form表单的输入项
            List<FileItem> list = upload.parseRequest(request);
            for(FileItem item : list){
                //如果fileitem中封装的是普通输入项的数据
                if(item.isFormField()){
                    String name = item.getFieldName();
                    //解决普通输入项的数据的中文乱码问题
                    String value = item.getString("UTF-8");
                    //value = new String(value.getBytes("iso8859-1"),"UTF-8");
                }else{//如果fileitem中封装的是上传文件
                    //得到上传的文件名称，
                    String filename = item.getName();
                    if(filename == null || filename.trim().equals("")){
                        continue;
                    }
                    UploadManager uploadManager = new UploadManager(new Configuration(Zone.zone1()));
                    Response response = uploadManager.put(item.getInputStream(),null,upToken,null, null);
                    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

                    item.delete();
                    return new ComResult<String>(true,putRet.key,"");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();

        }
        return new ComResult<String>(true,"","上传失败");
    }
}